//
//  GAButtonViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/1/25.
//  Copyright © 2021 热带雨林. All rights reserved.
//

#import "GAButtonViewController.h"

BOOL isClick = NO;

@interface GAButtonViewController ()<UIViewGDButtonDelegate>

@property (nonatomic, strong) GDButton *btn; //左右
@property (nonatomic, strong) GDButton *btnII; //上下
@property (nonatomic, strong) UIView *testBtnView;
@property (nonatomic, assign) CGRect rect;
@property (nonatomic, strong) GDButton *testBtn;
@property (nonatomic, strong) UIButton *button;

@end


@implementation GAButtonViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    //左右
    self.btn = [[GDButton alloc]initWithFrame:CGRectZero];
    [self.btn setTitle:@"changeBtn" forState:UIControlStateNormal];
    [self.btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.btn setImage:[UIImage imageNamed:@"APC_timeLineIcon"] forState:UIControlStateNormal];
    [self.view addSubview:self.btn];
    
    //左文字 右图片
    self.btn.titleRect = CGRectMake(10.0, 10.0, 100.0, 30.0);
    self.btn.imageRect = CGRectMake(100.0, 10.0, 30.0, 30.0);
    
    //左图片 右文字
//    self.btn.titleRect = CGRectMake(40.0, 10.0, 100.0, 30.0);
//    self.btn.imageRect = CGRectMake(10.0, 10.0, 30.0, 30.0);
    self.btn.layer.cornerRadius = 5.0;
    self.btn.layer.masksToBounds = YES;
    
    //按钮边框
    self.btn.layer.borderWidth = 1.0f;
    self.btn.layer.borderColor = [UIColor redColor].CGColor;
    
    // 立即得出btn自适应后的frame
    [self.btn layoutIfNeeded];
    
    //开启避免按钮频繁点击
    [UIControl ga_exchangeClickMethod];
    self.btn.ignoreClickInterval = NO;//忽略间隔时间 YES设置间隔时间无效
    self.btn.clickInterval = 1.5; //设置间隔时间
    
    
    [self.btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(300);
        make.left.equalTo(self.view).offset(30);
        //设置宽高无效
//        make.width.mas_equalTo(300);
//        make.height.mas_equalTo(90);
    }];
    
    //上下
    self.btnII = [[GDButton alloc]initWithFrame:CGRectZero];
    [self.btnII setTitle:@"changeBtn" forState:UIControlStateNormal];
    [self.btnII setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.btnII setImage:[UIImage imageNamed:@"APC_timeLineIcon"] forState:UIControlStateNormal];
    [self.view addSubview:self.btnII];
    
    
    //文字上 图片下
//    self.btnII.titleRect =  CGRectMake(10.0, 7.0, 90.0, 30);
//    self.btnII.imageRect = CGRectMake(45, 40, 30, 30);
    //图片下 文字下
    self.btnII.titleRect = CGRectMake(10.0, 40, 90.0, 30);
    self.btnII.imageRect = CGRectMake(45.0, 10.0, 30.0, 30.0);
    self.btnII.layer.cornerRadius = 5.0f;
    self.btnII.layer.masksToBounds = YES;
    self.btnII.layer.borderWidth = 1.0f;
    self.btnII.layer.borderColor = [UIColor redColor].CGColor;
    // 立即得出btn自适应后的frame
    [self.btnII layoutIfNeeded];
    
    
    //防止按钮被多次点击
    self.btnII.ignoreClickInterval = NO;
    self.btnII.clickInterval = 3;

    [self.btnII addTarget:self action:@selector(changebtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.btnII mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(300);
        make.right.equalTo(self.view.mas_right).offset(-30);
    }];
    

    //按钮合集
    self.testBtnView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:self.testBtnView];
//    self.testBtnView.delegate = self;
    
    NSArray *titleAry = @[@"克鲁斯",@"啥就开",@"那可就",@"这就是",@"等哈说楼房吉安老师发货啦",@"盛开的花",
    @"就发生；楼房吉安；十分骄傲；十分骄傲；是否"];
    
    GDWeakSelf;
    //通过block返回整个buttonView的rect
    [self.testBtnView creatNormalButtonWithTitleArray:titleAry withRect:^(CGRect rect) {
        weakSelf.rect = rect;
    }];
    
    [self.testBtnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(SJHeight);
        make.left.equalTo(self.view).offset(20);
        make.size.mas_equalTo(CGSizeMake(self.rect.size.width, self.rect.size.height));
    }];
    
    //解决循环引用
    @weakify(self);
    //代替代理  不用再写代理了
    [[self.testBtnView rac_signalForSelector:@selector(clickButtonAction:)] subscribeNext:^(RACTuple * _Nullable x) {
        @strongify(self);
        // RACTupleUnpack 可以将返回值的 id值 x转成 所所需要的类型
        RACTupleUnpack(UIButton *button) = x;
        self.button = button;
        isClick = YES;
        [self clickButtonWithIndex:self.button.tag];
        NSLog(@"点击了按钮%ld",button.tag);
    }];
    
    
    //监听按钮点击事件
    [[self.btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//        RACTupleUnpack(UIButton *button) = x;
        
        NSLog(@"点击了左右布局按钮");
    }];
    
    [[self.btnII rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"点击了上下布局按钮");
    }];
}


- (void)clickButtonWithIndex:(NSInteger)index{
    
}

//
- (void)test{
    NSLog(@"点击了点赞按钮");
}


//因为是控制器不是视图，所有没有响应分类中的方法
- (void)clickButtonAction:(UIButton *)sender{
    NSLog(@"%ld",sender.tag);
}


- (void)click:(UIButton *)sender{
    sender.selected = !sender.selected;
    NSLog(@"111");
}


- (void)changebtn:(UIButton *)sender{
    NSLog(@"222");
}
//
- (void)dealloc{
    NSLog(@"HH");
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


@end
